package com.yyj.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.yyj.common.BaseResponse;
import com.yyj.constant.WebConstant;
import com.yyj.pojo.user.SessionInfo;
import com.yyj.service.PaymentService;
import com.yyj.service.WaterBillService;
import com.yyj.util.DateUtil;
import com.yyj.util.RspUtils;
import com.yyj.web.protocol.req.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value = "水费相关接口" , description="水费相关接口")
@RestController
public class WaterBillContoller extends BaseController {

    @Autowired
    WaterBillService waterBillService;

    @ApiOperation(value = "业主查看水费信息")
    @RequestMapping(value = "/auth/waterBills", method = RequestMethod.GET)
    public BaseResponse queryWaterBill(WaterBillQueryReq waterBillQueryReq
    ){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            waterBillQueryReq.setRoomNum(sessionInfo.getRoomNum());
            return RspUtils.success(waterBillService.queryWaterBillInfo(waterBillQueryReq));
        }catch(Exception e){
            logger.error("业主查看水费信息异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "业主查看水表数据详情")
    @RequestMapping(value = "/auth/waterBills/detail/{id}", method = RequestMethod.GET)
    public BaseResponse queryWaterBillDetail(@PathVariable long id
    ){
        try{
            return RspUtils.success(waterBillService.queryWaterBillDetail(id));
        }catch(Exception e){
            logger.error("业主查看水费信息异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "判断业主当月是否提交过水表数据")
    @RequestMapping(value = "/auth/waterBills/checkStatus", method = RequestMethod.GET)
    public BaseResponse fillWaterBill(
    ){
        try{
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            JSONObject jsonData = new JSONObject();
            jsonData.put("isSubmit" , waterBillService.checkWaterBill(sessionInfo));
            return RspUtils.success(jsonData);
        }catch(Exception e){
            logger.error("业主填写水表数据异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "业主填写水表数据")
    @RequestMapping(value = "/auth/waterBills/fill", method = RequestMethod.POST)
    public BaseResponse fillWaterBill(@RequestBody @Validated WaterBillAddReq waterBillAddReq , BindingResult result
    ){
        try{
            checkRequestParams(result);
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            return RspUtils.success(waterBillService.fillWaterBill(waterBillAddReq , sessionInfo));
        }catch(Exception e){
            logger.error("业主填写水表数据异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

    @ApiOperation(value = "业主编辑水表数据")
    @RequestMapping(value = "/auth/waterBills/update", method = RequestMethod.POST)
    public BaseResponse updateWaterBill(@RequestBody @Validated WaterBillUpdateReq waterBillUpdateReq , BindingResult result
    ){
        try{
            checkRequestParams(result);
            SessionInfo sessionInfo = (SessionInfo)session.getAttribute(WebConstant.SESSION_INFO);
            return RspUtils.success(waterBillService.updateWaterBill(waterBillUpdateReq , sessionInfo));
        }catch(Exception e){
            logger.error("业主编辑水表数据异常：{}" , e.getMessage() , e);
            return RspUtils.error(e.getMessage());
        }
    }

}
